// pages/foodDetail/foodDetail.js
import {
  findCanteenNameById
} from "../../apis/apis"
var app=getApp()
let out_options;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentFoodId:0,
    foodDetail:{},
    but:true,
    invalue:"",
    commentArr:[],
    loginState:false,
    userRating:4,
    pageNo:1,
    timeShow:"",
    site:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    out_options=options
    this.setData({
      currentFoodId:options.id
    })
    this.getFoodDetail(options);
    this.getFoodComment(out_options);
  },
  getFoodDetail:function(e){
    wx.request({
      url: app.globalData.apiUrl+'/food/getFoodInfoById',
      data: {
        id:parseInt(e.id)
      },
      method: "POST",
      success: (result) => {
        var newFoodDetail=result.data.data.data
        this.setData({
          foodDetail:newFoodDetail
        })
        if(this.data.foodDetail.time.indexOf("1")!=-1){
          this.setData({
            timeShow:this.data.timeShow+"早餐 "
          })
        }
        if(this.data.foodDetail.time.indexOf("2")!=-1){
          this.setData({
            timeShow:this.data.timeShow+"午餐 "
          })
        }
        if(this.data.foodDetail.time.indexOf("3")!=-1){
          this.setData({
            timeShow:this.data.timeShow+"晚餐 "
          })
        }
        if(this.data.foodDetail.time.indexOf("4")!=-1){
          this.setData({
            timeShow:this.data.timeShow+"宵夜"
          })
        }
        console.log(this.data.foodDetail.canteenId);
        this.setData({
          site:findCanteenNameById(this.data.foodDetail.canteenId,app.globalData.canteenList)
        })
        wx.setNavigationBarTitle({
          title:this.data.foodDetail.title
        })
      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  getFoodComment:function(e){
    wx.request({
      url: app.globalData.apiUrl+'/comment/getCommentList',
      data: {
        "foodId":parseInt(e.id),
        "pageSize":5,
        "pageNo":this.data.pageNo
      },
      method: "POST",
      success: (result) => {
        var newCommentArr=result.data.data.data;
        for (let index = 0; index < newCommentArr.length; index++) {
          // console.log(newCommentArr[index].commentDate.slice(0,10));
          //截取时间的前几位
          newCommentArr[index].commentDate=newCommentArr[index].commentDate.slice(0,10)
        }
        this.setData({
          commentArr:newCommentArr
        })

      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  buttonShow:function(e){
    // console.log(e.detail.value)

    if (e.detail.value.trim() != "" && wx.getStorageSync('token')!= "") {
      this.setData({
        invalue:e.detail.value,
        but:false
      })
    }
    else{
      this.setData({
        but:true
      })
    }
  },
  ratingChange:function(e){
    this.setData({
      userRating:e.detail
    })
  },
  postComment(){
    if(this.data.invalue !="" && this.data.but==false){
      wx.request({
        url: app.globalData.apiUrl+'/comment/addComment',
        data: {
          "token":wx.getStorageSync('token'),
          "foodId":parseInt(this.data.currentFoodId),
          "rating":parseInt(this.data.userRating),
          "commentDetail":this.data.invalue.trim()
        },
        method: "POST",
        success: (result) => {
          console.log(result);
          wx.showToast({
            title: result.data.message
          })
          this.setData({
            invalue:""
          });
          this.getFoodComment(out_options);
        }
      })
      this.setData({
        pageNo:1
      })
      wx.request({
        url: app.globalData.apiUrl+'/comment/getCommentList',
        data: {
          "foodId":parseInt(currentFoodId),
          "pageSize":5,
          "pageNo":this.data.pageNo
        },
        method: "POST",
        success: (result) => {
          var newCommentArr=result.data.data.data;
          for (let index = 0; index < newCommentArr.length; index++) {
            // console.log(newCommentArr[index].commentDate.slice(0,10));
            //截取时间的前几位
            newCommentArr[index].commentDate=newCommentArr[index].commentDate.slice(0,10)
          }
          this.setData({
            commentArr:result.data.data.data
          })
  
        },
        fail: (err) => {},
        complete: (res) => {},
      })
    }
    else{
      wx.showToast({
        title: '内容不能为空',
        icon:'error'
      })
    }
  },
  goLogin(){
    wx.navigateTo({
      url: '../../pages/registerAndLogin/registerAndLogin',
    })
  },
  //触底加载评论
  continueLoadComment(){
    console.log("continue");
    this.setData({
      pageNo:this.data.pageNo+1
    })
    wx.request({
      url: app.globalData.apiUrl+'/comment/getCommentList',
      data: {
        "foodId":parseInt(this.data.currentFoodId),
        "pageSize":5,
        "pageNo":this.data.pageNo
      },
      method: "POST",
      success: (result) => {
        var newCommentArr=result.data.data.data;
        for (let index = 0; index < newCommentArr.length; index++) {
          // console.log(newCommentArr[index].commentDate.slice(0,10));
          //截取时间的前几位
          newCommentArr[index].commentDate=newCommentArr[index].commentDate.slice(0,10)
        }
        let oldCommentArr=this.data.commentArr
        let newArr=oldCommentArr.concat(newCommentArr)
        this.setData({
          commentArr:newArr
        })

      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      loginState:app.globalData.userInfo.loginState
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.continueLoadComment();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})